package flc.ast.dialog;

import android.app.Activity;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.ToastUtils;

import flc.ast.R;
import flc.ast.databinding.DialogRecordBinding;
import flc.ast.utils.Content;
import stark.common.basic.base.BaseEventDialog;

public class RecordDialog extends BaseEventDialog<DialogRecordBinding> implements View.OnClickListener {

    private String currentName;
    private int currentType;

    public RecordDialog(@NonNull Activity activity) {
        super(activity);
    }

    public void setCurrentFileName(String fileName) {
        currentName = fileName;
    }

    public void setType(int type) {
        currentType = type;
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.dialog_record;
    }

    @Override
    protected void initContentView(View view) {
        switch (currentType) {
            case Content.record_type_create:
                mContentDataBinding.tvRecordTitle.setText(R.string.please_input_name);
                break;
            case Content.record_type_rename:
                mContentDataBinding.tvRecordTitle.setText(R.string.rename_name);
                break;
        }
        mContentDataBinding.etRecordName.setText(currentName);

        mContentDataBinding.ivRecordCancel.setOnClickListener(this);
        mContentDataBinding.ivRecordConfirm.setOnClickListener(this);

        int mMaxNum = 24;  // 输入框最大值
        mContentDataBinding.etRecordName.addTextChangedListener(new TextWatcher() {
            //记录输入的字数
            private CharSequence wordNum;

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // 实时记录输入的字数
                wordNum = s;
            }

            @Override
            public void afterTextChanged(Editable s) {
                int selectionStart = mContentDataBinding.etRecordName.getSelectionStart();
                int selectionEnd = mContentDataBinding.etRecordName.getSelectionEnd();
                // 判断大于最大值
                if (wordNum.length() > mMaxNum) {
                    // 删除多余输入的字（不会显示出来）
                    s.delete(selectionStart - 1, selectionEnd);
                    mContentDataBinding.etRecordName.setText(s);
                    mContentDataBinding.etRecordName.setSelection(mMaxNum);  // 设置光标在最后
                    ToastUtils.showShort(R.string.et_name_max_tips);
                }
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ivRecordCancel:
                dismiss();
                break;
            case R.id.ivRecordConfirm:
                String content = mContentDataBinding.etRecordName.getText().toString();
                if (TextUtils.isEmpty(content)) {
                    ToastUtils.showShort(R.string.please_input_name);
                    return;
                }
                if (listener != null) {
                    listener.onClickConfirm(content);
                }
                break;
        }
    }

    private IListener listener;

    public void setListener(IListener listener) {
        this.listener = listener;
    }

    public interface IListener {
        void onClickConfirm(String name);
    }
}
